package com.example.kotlintest.chat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.guoxiaoxing.phoenix.core.PhoenixOption;
import com.guoxiaoxing.phoenix.core.listener.ImageLoader;
import com.guoxiaoxing.phoenix.core.model.MediaEntity;
import com.guoxiaoxing.phoenix.core.model.MimeType;
import com.guoxiaoxing.phoenix.picker.Phoenix;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author linzi
 * @date 2022-9-28
 */
public class PhotoPicker extends AppCompatActivity {

    public interface photoPickerBack {
        void getFinalPath(List<String>realPath, List<String>cutPath, List<String>compress);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        List<MediaEntity> mediaEntities = new ArrayList<MediaEntity>();
        if (values != null) {
            for (String path : values) {
                MediaEntity mediaEntity =new MediaEntity();
                mediaEntity.setLocalPath(path);
                mediaEntities.add(mediaEntity);
            }
        }
        Phoenix.with()
                .theme(PhoenixOption.THEME_DEFAULT)// 主题
                .fileType(fileType)//显示的文件类型图片、视频、图片和视频
                .maxPickNumber(maxPickNumber)// 最大选择数量
                .minPickNumber(minPickNumber)// 最小选择数量
                .spanCount(spanCount)// 每行显示个数
                .enablePreview(enablePreview)// 是否开启预览
                .enableCamera(enableCamera)// 是否开启拍照
                .enableAnimation(enableAnimation)// 选择界面图片点击效果
                .enableCompress(enableCompress)// 是否开启压缩
                .compressPictureFilterSize(compressPictureFilterSize)//多少kb以下的图片不压缩
                .compressVideoFilterSize(compressVideoFilterSize)//多少kb以下的视频不压缩
                .thumbnailHeight(thumbnailSize)// 选择界面图片高度
                .thumbnailWidth(thumbnailSize)// 选择界面图片宽度
                .enableClickSound(false)// 是否开启点击声音
                .pickedMediaList(mediaEntities)// 已选图片数据
                .videoFilterTime(videoFilterTime)//显示多少秒以内的视频
                .mediaFilterSize(mediaFilterSize)//显示多少kb以下的图片/视频，默认为0，表示不限制
                //如果是在Activity里使用就传Activity，如果是在Fragment里使用就传Fragment
                .start(this, type == 0? PhoenixOption.TYPE_PICK_MEDIA : PhoenixOption.TYPE_TAKE_PICTURE, REQUEST_CODE);

    }

    public PhotoPicker  setThemes(int theme) {
        PhotoPicker.theme = theme;
        return this;
    }

    public PhotoPicker  setFileType(int fileType) {
        PhotoPicker.fileType = fileType;
        return this;
    }

    public PhotoPicker  setMaxPickNumber(int maxPickNumber) {
        PhotoPicker.maxPickNumber = maxPickNumber;
        return this;
    }

    public PhotoPicker  setMinPickNumber(int minPickNumber) {
        PhotoPicker.minPickNumber = minPickNumber;
        return this;
    }

    public PhotoPicker  setSpanCount(int spanCount) {
        PhotoPicker.spanCount = spanCount;
        return this;
    }

    public PhotoPicker  setEnablePreview(boolean enablePreview) {
        PhotoPicker.enablePreview = enablePreview;
        return this;
    }

    public PhotoPicker  setEnableCamera(boolean enableCamera) {
        PhotoPicker.enableCamera = enableCamera;
        return this;
    }

    public PhotoPicker  setEnableAnimation(boolean enableAnimation) {
        PhotoPicker.enableAnimation = enableAnimation;
        return this;
    }

    public PhotoPicker  setEnableCompress(boolean enableCompress) {
        PhotoPicker.enableCompress = enableCompress;
        return this;
    }

    public PhotoPicker  setCompressPictureFilterSize(int compressPictureFilterSize) {
        PhotoPicker.compressPictureFilterSize = compressPictureFilterSize;
        return this;
    }

    public PhotoPicker  setCompressVideoFilterSize(int compressVideoFilterSize) {
        PhotoPicker.compressVideoFilterSize = compressVideoFilterSize;
        return this;
    }

    public PhotoPicker  setThumbnailSize(int thumbnailSize) {
        PhotoPicker.thumbnailSize = thumbnailSize;
        return this;
    }

    public PhotoPicker  setVideoFilterTime(int videoFilterTime) {
        PhotoPicker.videoFilterTime = videoFilterTime;
        return this;
    }

    public PhotoPicker  setMediaFilterSize(int mediaFilterSize) {
        PhotoPicker.mediaFilterSize = mediaFilterSize;
        return this;
    }

    public PhotoPicker  setCallback(photoPickerBack callback){
        PhotoPicker.callback = callback;
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PhotoPicker.theme = PhoenixOption.THEME_DEFAULT;
        PhotoPicker.fileType = MimeType.ofImage();
        PhotoPicker.maxPickNumber = 9;
        PhotoPicker.minPickNumber = 0;
        PhotoPicker.spanCount = 4;
        PhotoPicker.enablePreview = true;
        PhotoPicker.enableCamera = true;
        PhotoPicker.enableAnimation = true;
        PhotoPicker.enableCompress = false;
        PhotoPicker.compressPictureFilterSize = 1024;
        PhotoPicker.compressVideoFilterSize = 2048;
        PhotoPicker.thumbnailSize = 160;
        PhotoPicker.videoFilterTime = 0;
        PhotoPicker.mediaFilterSize = 0;
        PhotoPicker.callback = null;
        PhotoPicker.values = null;
        PhotoPicker.type=0;
        PhotoPicker.isStop=false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            List<MediaEntity> result = Phoenix.result(data);
            List<String> realPath = new ArrayList<String>();
            List<String>  cutPath = new ArrayList<String>();
            List<String>  compress = new ArrayList<String>();
            if (type == 0) {
                for (MediaEntity mediaEntity : result) {
                    if (mediaEntity.getLocalPath() != null) {
                        realPath.add(mediaEntity.getLocalPath());
                    }
                    if (mediaEntity.getCutPath() != null) {
                        cutPath.add(mediaEntity.getCutPath());
                    }
                    if (mediaEntity.getCompressPath() != null) {
                        compress.add(mediaEntity.getCompressPath());
                    }
                }
                callback.getFinalPath(realPath, cutPath, compress);
            }else {
                realPath.addAll(values);
                for (MediaEntity mediaEntity : result) {
                    realPath.add(mediaEntity.getLocalPath());
                }
                Log.d("选择图片", realPath.toString());
                callback.getFinalPath(realPath, cutPath, compress);
            }
        }
        finish();
    }

    private final static String TAG = "PhotoPicker";
    private  static int theme = PhoenixOption.THEME_DEFAULT;
    private  static int fileType = MimeType.ofImage();
    private  static int maxPickNumber = 9;
    private  static int  minPickNumber = 0;
    private  static int spanCount = 4;
    private  static boolean enablePreview = true;
    private  static boolean enableCamera = true;
    private  static boolean enableAnimation = true;
    private  static boolean enableCompress = false;
    private  static int compressPictureFilterSize = 1024;
    private  static int compressVideoFilterSize = 2048;
    private  static int thumbnailSize = 160;
    private  static int videoFilterTime = 0;
    private  static int mediaFilterSize = 0;
    private  static int REQUEST_CODE = 10001;
    private  static  photoPickerBack callback = null;

    private static boolean isInit = false;
    private  static ArrayList<String> values = null;
    private  static int type=0; //type==0  相册，1相机

    private  static boolean isStop=false;

    public static void init(ImageLoader imageLoad){
        Phoenix.config()
                .imageLoader(imageLoad);
        isInit = true;
    }

    public static void show(Context context, int type, ArrayList<String> values) {
        if (!isInit) {
            Log.e(TAG, "show: 工具未初始化");
            return;
        }
        if (callback == null) {
            Log.e(TAG, "show: 未设置回调");
            return;
        }

        PhotoPicker.values = values;
        PhotoPicker.type = type;

        Intent intent = new Intent(context, PhotoPicker.class);
        context.startActivity(intent);

    }
}
